package com.xiaoyao.novel.pojo;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;


import com.mysql.jdbc.StringUtils;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.DateUtil;
import com.xiaoyao.novel.util.FileUtil;
import com.xiaoyao.novel.util.HtmlStatic;

public class Book {

	public String bookPath = FileUtil.bookPath;

	public static final String TYPE_QIDIAN = "qidian"; // 起点
	public static final String TYPE_QIDIANMM = "qdmm"; // 起点MM
	public static final String TYPE_SHUHUANG = "shuhuang"; // 书荒/; // 宋朝文学
	public static final String TYPE_ZONGHENG = "zongheng"; // 纵横
	public static final String TYPE_CHUANGSHI = "chuangshi"; // 创世 QQ
	public static final String TYPE_BIQUGE = "biquge"; // 笔趣阁
	public static final String TYPE_K16 = "k16"; // 16book
	public static final String TYPE_K17 = "k17"; // 17k
	public static final String TYPE_PIAOTIAN = "piaotian"; // 飘天
	public static final String TYPE_SILUKE = "siluke"; // 丝路客
	public static final String TYPE_CHANGXIANG = "changxiang"; // 畅想
	public static final String TYPE_TADU = "tadu"; // 塔读
	public static final String TYPE_QUDUDU = "qududu"; // 去读读
	public static final String TYPE_QUDUWU = "quduwu"; // 去读屋

	public static final String TYPE_DU5 = "du5"; // 5读5
	public static final String TYPE_PINNONG = "pinnong"; // 贫农
	public static final String TYPE_ZW69 = "zw69"; // 69中文
	public static final String TYPE_BIQI = "biqi"; // 比奇中文
	public static final String TYPE_GO114 = "go114"; // 114书苑

	public static final String TYPE_BBQUGE = "bbquge"; // bbquge
	public static final String TYPE_QIUWU = "qiuwu"; // 凤舞文学

	public static final String TYPE_MT62 = "mt62"; // 墨坛文学
	public static final String TYPE_FENGYUN = "baoliny"; // 风云小说
	public static final String TYPE_TIANJIN = "tianjin"; //天津小说
	
	public static final String TYPE_IREADER = "ireader"; //掌阅小说
	public static final String TYPE_773BUY = "773buy"; //燃文小说

	public String crawler = TYPE_K16; // 爬虫类型
	public int bookId;// ID
	public String webId = ""; // 爬取网站的ID
	public String bookName = "";// 书名
	public String authorName = "";// 作者
	public String describ = "";// 简介
	public String picPath = "";// 图片
	public String description = "";
	public String category = ""; // 分类
	public String keywords = "";
	public int savedCount = 0;
	public int clickCount = 0; // 点击
	public int isRecommend = 0; // 是否推荐
	public int isOver = 0; // 是否完结
	public Timestamp updateTime = DateUtil.getNowTime();
	public List<Chapter> list = new ArrayList<Chapter>();// /
	public String newChapter = "";
	public int vip = 0; // 是否VIP 了
	public String lastDate = "";// 网页最后修改时间

	// 非持久化的数据
	public String bookUrl = "";// 宋朝文学辅助字段.
	public String shortDescrib = "";
	public int shortSize = 70;

	public String categoryCacheFile = "";
	public String getCategoryCache() {
		categoryCacheFile = FileUtil.getWebRoot() + FileUtil.bookCachePath + "c-" + bookId + ".html";
		return categoryCacheFile;
	}
	public String html;

	public String getHtml() {
		return HtmlStatic.encodeBook(bookId);
	}
	public String getLastDate() {
		return lastDate;
	}
	public void setLastDate(String lastDate) {
		this.lastDate = lastDate;
	}
	public void setHtml(String html) {
		this.html = html;
	}
	public String getBookPath() {
		return bookPath;
	}
	public String getCrawler() {
		return crawler;
	}

	public void setCrawler(String crawler) {
		this.crawler = crawler;
	}

	public int getBookId() {
		return bookId;
	}

	public void setBookId(int bookId) {
		this.bookId = bookId;
	}

	public String getWebId() {
		return webId;
	}

	public void setWebId(String webId) {
		this.webId = webId;
	}

	public String getBookName() {
		return bookName.trim();
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public String getAuthorName() {
		return authorName;
	}

	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}

	public String getDescrib() {
		if (describ.length() > 500) {
			describ = describ.substring(0, 500);
		}
		try {
			// System.out.println("describ:"+describ);
			describ = describ.replaceAll("<!--.*?-->", "");
			describ = URLEncoder.encode(describ);
			describ = describ.replaceAll("%E2%8B%AF", "");
			describ = describ.replaceAll("%C2%A0", "");
			// describ=describ.replaceAll("\\\\x.{2}","");
			describ = URLDecoder.decode(describ);
		} catch (Exception e) {
			System.err.println(e);;
		}
//		try {
//			// System.out.println("describ:"+describ);
//			describ = describ.replaceAll("<!--.*?-->", "");
//			describ = URLEncoder.encode(describ);
//			describ = describ.replaceAll("%E2%8B%AF", "");
//			describ = describ.replaceAll("%C2%A0", "");
//			// describ=describ.replaceAll("\\\\x.{2}","");
//			describ = URLDecoder.decode(describ);
//		} catch (Exception e) {
//			System.err.println(e);; 
//		}
		return describ;
	}

	public void setDescrib(String describ) {
		this.describ = describ;
		if (ClassUtil.isEmpty(this.describ )) {
			this.describ = describ;
		}
	}

	public String getPicPath() {
		return picPath;
	}

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}

	public String getDescription() { 
		if (ClassUtil.isEmpty( description)) {
			description = describ;
		}
		try {
			description = description.replaceAll("<.+?>", "");
			description = URLEncoder.encode(description);
			description = description.replaceAll("%E2%8B%AF", "");
			description = description.replaceAll("%C2%A0", "");
			description = URLDecoder.decode(description);
		} catch (Exception e) {
			System.err.println(e);;
		}
//		try {
//			description = description.replaceAll("<.+?>", "");
//			description = URLEncoder.encode(description);
//			description = description.replaceAll("%E2%8B%AF", "");
//			description = description.replaceAll("%C2%A0", "");
//			description = URLDecoder.decode(description);
//		} catch (Exception e) {
//			System.err.println(e);;
//		}
		return description ;
		}

	public void setDescription(String description) {
		this.description = description;
		if (ClassUtil.isEmpty(this. description)) {
			this.description = description;
		}
	}

	public String getKeywords() {
		if (ClassUtil.isEmpty(keywords)) {
			keywords = bookName;
		}
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public int getSavedCount() {
		return savedCount;
	}

	public void setSavedCount(int savedCount) {
		this.savedCount = savedCount;
	}

	public Timestamp getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Timestamp updateTime) {
		this.updateTime = updateTime;
	}

	public List<Chapter> getList() {
		return list;
	}

	public void setList(List<Chapter> list) {
		this.list = list;
	}

	public String getBookUrl() {
		return bookUrl;
	}

	public void setBookUrl(String bookUrl) {
		this.bookUrl = bookUrl;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public int getClickCount() {
		return clickCount;
	}

	public void setClickCount(int clickCount) {
		this.clickCount = clickCount;
	}

	public int getIsRecommend() {
		return isRecommend;
	}

	public void setIsRecommend(int isRecommend) {
		this.isRecommend = isRecommend;
	}

	public int getIsOver() {
		return isOver;
	}

	public void setIsOver(int isOver) {
		this.isOver = isOver;
	}

	public String getNewChapter() {
		newChapter = newChapter.replaceFirst("第.+?卷.*?第", "第");
		return newChapter;
	}

	public void setNewChapter(String newChapter) {
		this.newChapter = newChapter;
	}

	public int getShortSize() {
		return shortSize;
	}

	public void setShortSize(int shortSize) {
		this.shortSize = shortSize;
	}

	public String getShortDescrib() {
		if (describ.length() < shortSize) {
			shortSize = describ.length();
		}
		shortDescrib = describ.replaceAll("&nbsp;", " ").replaceAll("<.+?>", "").substring(0, shortSize) + "...";
		return shortDescrib;
	}

	public void setShortDescrib(String shortDescrib) {
		this.shortDescrib = shortDescrib;
	}

	@Override
	public String toString() {
		return "Book [authorName=" + authorName + ", bookId=" + bookId + ", bookName=" + bookName + ", bookUrl="
				+ bookUrl + ", category=" + category + ", clickCount=" + clickCount + ", crawler=" + crawler
				+ ", describ=" + describ + ", description=" + description + ", html=" + html + ", isOver=" + isOver
				+ ", isRecommend=" + isRecommend + ", keywords=" + keywords + ", list=" + list + ", newChapter="
				+ newChapter + ", picPath=" + picPath + ", savedCount=" + savedCount + ", shortDescrib=" + shortDescrib
				+ ", shortSize=" + shortSize + ", updateTime=" + updateTime + ", webId=" + webId + "]";
	}

	public int getVip() {
		return vip;
	}

	public void setVip(int vip) {
		this.vip = vip;
	}

}